'use strict'

define(['jquery', 'bootstrap', 'datetimepicker', 'datetimepickerCN', 
        'select2', 'shiftform', 'validate', 'validate.en'], function($) {
  
  return {
        
    init: function(data) {
      this.create();
      this.edit();
      this.delete();
      this.exportReport(data.today);
    },
    
    create: function() {
    	
      var that = this;
    	
      $(document).on('shown.bs.modal', '#record-create', function(e){
        
        // bind exist user function 
        that.existUser(e);
      	
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
        
        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        $form.shiftform({
          beforeSubmit : function(el) {
        	$form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id];
          }
        })
        
      })
    },
    
    existUser: function(e) {
      $(e.currentTarget).find('.user-search').on('click', function(){
        
        var mobile = $(e.currentTarget).find('input[name="mobile"]').val();
        if (mobile !== undefined && mobile !== null && mobile) {
          
          $.ajax({
            url: window.__CTX__.root + "/users/exist",
            type: 'POST',
            data: {'mobile': mobile},
            cache: false
          }).done(function(result) {
            result = JSON.parse(result);
            $(e.currentTarget).find('.search-block').css('display', '');
            if (result.code === '1') {  // not exist user, do create
              $(e.currentTarget).find('.search-result').text('用户不存在，可创建访客记录').css({'color': 'blue', 'font-weight': 'bolder'});
              $(e.currentTarget).find('.btn-success').removeClass("disabled")
            } else if (result.code === '0') { // already exist user, can not create
              $(e.currentTarget).find('.search-result').text('该用户已存在, 无法创建访客记录').css({'color': 'red', 'font-weight': 'bolder'});
              $(e.currentTarget).find('.btn-success').addClass('disabled');
            }
          });
          
        }
        
      });
    },
      
    edit: function() {
    	var that = this;
      $(document).on('shown.bs.modal', '#record-edit', function(e){
      	
    	$(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
        
        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        $form.shiftform({
          beforeSubmit : function(el) {
        	$form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id];
          }
        })
      })
    },
      
    delete: function() {
      $(document).on('shown.bs.modal', '#record-delete', function(e){
    	  $(e.currentTarget).find("form").shiftform() 
      })
    },
    
    exportReport: function(today) {
        var that = this;
        
        $(document).on('shown.bs.modal', '#export', function(e){
          $('.date').datetimepicker({
                format: 'yyyy-mm-dd',
                autoclose: true,
                language: 'zh-CN',
                todayBtn: 'linked',
                todayHighlight: true,
                pickerPosition: 'bottom-right',
                minView: 'month',
                startDate: '2017-01-01',
                endDate: today
          });
          
          $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
          
          $('button[type="submit"').click(function() {
              $(e.currentTarget).find("form").trigger("validate.ajax.submit");
              $.bt_validate.result[$.bt_validate.form_id];
          });
        })
      }
  }
})
